home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / autoinsert.el.z / autoinsert.el (.txt)
Encoding:
LaTeX Document  |  1998-10-28  |  8.8 KB  |  209 lines

  1. ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
  2. ;; Copyright (C) 1985, 1986, 1987, 1994, 1995 Free Software Foundation, Inc.
  3. ;; Author: Charlie Martin <crm@cs.duke.edu>
  4. ;; Adapted-By: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 2, or (at your option)
  9. ;; any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  16. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. ;; Boston, MA 02111-1307, USA.
  18. ;;; Commentary:
  19. ;;  The following defines an association list for text to be
  20. ;;  automatically inserted when a new file is created, and a function
  21. ;;  which automatically inserts these files; the idea is to insert
  22. ;;  default text much as the mode is automatically set using
  23. ;;  auto-mode-alist.
  24. ;;  To use: 
  25. ;;     (add-hook 'find-file-hooks 'auto-insert)
  26. ;;     setq auto-insert-directory to an appropriate slash-terminated value
  27. ;;  Author:  Charlie Martin
  28. ;;           Department of Computer Science and
  29. ;;           National Biomedical Simulation Resource
  30. ;;           Box 3709
  31. ;;           Duke University Medical Center
  32. ;;           Durham, NC 27710
  33. ;;          (crm@cs.duke.edu,mcnc!duke!crm) 
  34. ;;; Code:
  35. (defvar auto-insert 'not-modified
  36.   "*Controls automatic insertion into newly found empty files:
  37.     nil    do nothing
  38.     t    insert if possible
  39.     other    insert if possible, but mark as unmodified.
  40. Insertion is possible when something appropriate is found in
  41. `auto-insert-alist'.  When the insertion is marked as unmodified, you can
  42. save it with  \\[write-file] RET.
  43. This variable is used when `auto-insert' is called as a function, e.g.
  44. when you do (add-hook 'find-file-hooks 'auto-insert).
  45. With \\[auto-insert], this is always treated as if it were `t'.")
  46. (defvar auto-insert-query 'function
  47.   "*If non-`nil', ask user before auto-inserting.
  48. When this is `function', only ask when called non-interactively.")
  49. (defvar auto-insert-prompt "Perform %s auto-insertion? "
  50.   "*Prompt to use when querying whether to auto-insert.
  51. If this contains a %s, that will be replaced by the matching rule.")
  52. (defvar auto-insert-alist
  53.   '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header")
  54.      (upcase (concat (file-name-nondirectory
  55.               (substring buffer-file-name 0 (match-beginning 0)))
  56.              "_"
  57.              (substring buffer-file-name (1+ (match-beginning 0)))))
  58.      "#ifndef " str \n
  59.      "#define " str "\n\n"
  60.      _ "\n\n#endif")
  61.     (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program")
  62.      nil
  63.      "#include \""
  64.      ;; nop without latest cc-mode
  65.      (and (fboundp 'c-companion-file)
  66.       ;(file-readable-p (c-companion-file 'name))
  67.       (file-name-nondirectory (c-companion-file 'name))) & ?\"
  68.      | -10)
  69.     ("[Mm]akefile\\'" . "makefile.inc")
  70.     (html-mode . (lambda () (sgml-tag "html")))
  71.     (plain-tex-mode . "tex-insert.tex")
  72.     (bibtex-mode . "tex-insert.tex")
  73.     (latex-mode
  74.      ;; should try to offer completing read for these
  75.      "options, RET: "
  76.      "\\documentstyle[" str & ?\] | -1
  77.      ?{ (read-string "class: ") "}\n"
  78.      ("package, %s: "
  79.       "\\usepackage[" (read-string "options, RET: ") & ?\] | -1 ?{ str "}\n")
  80.      _ "\n\\begin{document}\n" _
  81.      "\n\\end{document}")
  82.     (("/bin/.*[^/]\\'" . "Shell-Script mode magic number")
  83.      lambda ()
  84.        (if (eq major-mode default-major-mode)
  85.      (sh-mode)))
  86.     (ada-mode . ada-header)
  87.     (("\\.el\\'" . "Emacs Lisp header")
  88.      "Short description: "
  89.      ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "
  90. ;; Copyright (C) " (substring (current-time-string) -4) " by "
  91.  (getenv "ORGANIZATION") | "Free Software Foundation, Inc." "
  92. ;; Author: " (user-full-name)
  93. '(if (search-backward "&" (save-excursion (beginning-of-line 1) (point)) t)
  94.      (replace-match (capitalize (user-login-name)) t t))
  95. '(end-of-line 1) " <" (user-login-name) ?@ (system-name) ">
  96. ;; Keywords: "
  97.  '(require 'finder)
  98.  ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
  99.  '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
  100.            finder-known-keywords)
  101.     v2 (mapconcat (lambda (x) (format "%10.0s:  %s" (car x) (cdr x)))
  102.        finder-known-keywords
  103.        "\n"))
  104.  ((let ((minibuffer-help-form v2))
  105.     (completing-read "Keyword, C-h: " v1 nil t))
  106.     str ", ") & -2 "
  107. ;; This file is part of GNU Emacs.
  108. ;; GNU Emacs is free software; you can redistribute it and/or modify
  109. ;; it under the terms of the GNU General Public License as published by
  110. ;; the Free Software Foundation; either version 2, or (at your option)
  111. ;; any later version.
  112. ;; GNU Emacs is distributed in the hope that it will be useful,
  113. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  114. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  115. ;; GNU General Public License for more details.
  116. ;; You should have received a copy of the GNU General Public License
  117. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  118. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  119. ;; Boston, MA 02111-1307, USA.
  120. ;;; Commentary:
  121. ;; " _ "
  122. ;;; Code:
  123. ;;; " (file-name-nondirectory (buffer-file-name)) " ends here"))
  124.   "A list specifying text to insert by default into a new file.
  125. Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
  126. CONDITION maybe a regexp that must match the new file's name, or it may be
  127. a symbol that must match the major mode for this element to apply.
  128. Only the first matching element is effective.
  129. Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
  130. ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
  131. file-name or one relative to `auto-insert-directory' or a function to call.
  132. ACTION may also be a vector containing several successive single actions as
  133. described above, e.g. [\"header.insert\" date-and-author-update].")
  134. ;; Establish a default value for auto-insert-directory
  135. (defvar auto-insert-directory "~/insert/"
  136.   "*Directory from which auto-inserted files are taken.")
  137. ;;;###autoload
  138. (defun auto-insert ()
  139.   "Insert default contents into a new file if `auto-insert' is non-nil.
  140. Matches the visited file name against the elements of `auto-insert-alist'."
  141.   (interactive)
  142.   (and (not buffer-read-only)
  143.        (or (eq this-command 'auto-insert)
  144.        (and auto-insert
  145.         (bobp) (eobp)))
  146.        (let ((alist auto-insert-alist)
  147.          case-fold-search cond desc action)
  148.      (goto-char 1)
  149.      ;; find first matching alist entry
  150.      (while alist
  151.        (if (atom (setq cond (car (car alist))))
  152.            (setq desc cond)
  153.          (setq desc (cdr cond)
  154.            cond (car cond)))
  155.        (if (if (symbolp cond)
  156.            (eq cond major-mode)
  157.          (string-match cond buffer-file-name))
  158.            (setq action (cdr (car alist))
  159.              alist nil)
  160.          (setq alist (cdr alist))))
  161.      ;; Now, if we found something, do it
  162.      (and action
  163.           (if (stringp action)
  164.           (file-readable-p (concat auto-insert-directory action))
  165.           (if auto-insert-query
  166.           (or (if (eq auto-insert-query 'function)
  167.               (eq this-command 'auto-insert))
  168.               (y-or-n-p (format auto-insert-prompt desc)))
  169.           (mapcar
  170.            (lambda (action)
  171.          (if (stringp action)
  172.              (if (file-readable-p
  173.               (setq action (concat auto-insert-directory action)))
  174.              (insert-file-contents action))
  175.            (save-window-excursion
  176.              ;; make buffer visible before skeleton or function
  177.              ;; which might ask the user for something
  178.              (switch-to-buffer (current-buffer))
  179.              (if (and (consp action)
  180.                   (not (eq (car action) 'lambda)))
  181.              (skeleton-insert action)
  182.                (funcall action)))))
  183.            (if (vectorp action)
  184.            action
  185.          (vector action))))
  186.      (and (buffer-modified-p)
  187.           (not (eq this-command 'auto-insert))
  188.           (set-buffer-modified-p (eq auto-insert t))))))
  189. ;;;###autoload
  190. (defun define-auto-insert (key action &optional after)
  191.   "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
  192. Optional AFTER means to insert action after all existing actions for CONDITION,
  193. or if CONDITION had no actions, after all other CONDITIONs."
  194.   (let ((elt (assoc key auto-insert-alist)))
  195.     (if elt
  196.     (setcdr elt
  197.         (if (vectorp (cdr elt))
  198.             (vconcat (if after (cdr elt))
  199.                  (if (vectorp action) action (vector action))
  200.                  (if after () (cdr elt)))
  201.           (if after
  202.               (vector (cdr elt) action)
  203.             (vector action (cdr elt)))))
  204.       (if after
  205.       (nconc auto-insert-alist (list (cons key action)))
  206.     (setq auto-insert-alist (cons (cons key action)
  207.                       auto-insert-alist))))))
  208. ;;; autoinsert.el ends here
  209.